<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="doserch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="120">
      </el-table-column>
      <el-table-column prop="name" label="部门名称" width="100">
      </el-table-column>
      <el-table-column prop="manager.username" label="部门管理员" width="100">
      </el-table-column>
      <el-table-column prop="parent.name" label="上级部门" width="120">
      </el-table-column>
      <el-table-column prop="state" label="当前状态" width="120">
        <template scope="scope">
          <div>
            <el-link type="success" v-if="scope.row.state==0">启用</el-link>
            <el-link type="danger" v-if="scope.row.state==-1">禁用</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit( scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel( scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="部门编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门名称">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="部门经理">
          <el-select v-model="editForm.manager" value-key="id" placeholder="请选择">
            <!--   v-for 遍历数据源  :key 每个选项唯一标识  :label 显示看 :value 选项值        -->
            <el-option
                v-for="item in emps"
                :key="item.id"
                :label="item.username"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级部门">
          <el-cascader  v-model="editForm.parent"
              :options="tree"
              :props="{ checkStrictly: true,value:'id',label:'name' }"
              clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
/*import util from '../../common/js/util'*/
//import NProgress from 'nprogress'
/*import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';*/

export default {
  data() {
    return {
      //高级条件搜索
      filters: {
        keyword: ""
      },
      //表格数据
      depts: [],
      //总条数
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //表格忙等
      listLoading: false,
      //批量删除搜集对象的数组
      sels: [],//列表选中列
      //编辑框
      editFormVisible: false,//编辑界面是否显示
      //编辑框忙等
      editLoading: false,
      //表单验证
      editFormRules: {
        sn: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //标题
      title: '',
      emps: [],//部门经理下拉框选择数据
      tree:[], //选择部门无限极树
      //编辑界面数据
      editForm: {
        sn: '',
        name: '',
        state: 0,
        manager: null,
        parent: null
      },

    }
  },
  methods: {
    doserch() {
      //this.currentPage=1,
      this.getdepts();
    },

    handleCurrentChange(val) {
      this.currentPage = val;
      this.getdepts();
    },
    //获取用户列表
    getdepts() {
      let para = {
        currentPage: this.currentPage,
        pagesize: this.pageSize,
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      //NProgress.start();
      this.$http.post('/dept', (para)).then((res) => {
        console.log(res.data);
        this.depts = res.data.query
        this.total = res.data.tatal
        this.listLoading = false;
      }).catch()

    },
    //删除
    handleDel(row) {
      console.log(row.id)
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();

        this.$http.delete('/dept/' + row.id).then((res) => {

          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: res.data.msg,
            type: res.data.success ? 'success' : error
          });
          this.getdepts();
        });
      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function ( row) {
      this.title = "编辑",
      this.editFormVisible = true;
      this.loadEmployee();
      this.loadTree()
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增",
      this.editForm = {
        sn: '',
        name: '',
        state: 0,
        manager: null,

      },
      this.loadEmployee();
      this.loadTree()
      this.editFormVisible = true
    },
    //编辑提交按钮
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let param = Object.assign({}, this.editForm);
            // [] 需要最后一个id
            let arr = param.parent;
            if( arr !=null ){
              let pid = arr[arr.length - 1];
              param.parent = {
                id: pid
              }
            }else{
              param.parent = null;
            }
            this.$http.put('/dept', param).then((res) => {
              this.editLoading = false;

              //NProgress.done();
              this.$message({
                message: '提交成功',
                type: 'success'
              });
              this.$refs['editForm'].resetFields();
              this.editFormVisible = false;
              this.getdepts();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        this.$http.patch('dept', ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: res.data.msg,
            type: res.data.success ? 'success' : error
          });
          this.getdepts();
        });
      }).catch(() => {

      });
    },
    //拉去员工列表
    loadEmployee() {
      this.$http.get('/emp').then((res) => {
       this.emps=res.data
      })
    },
    loadTree(){
      this.$http.get('/dept/tree').then((res)=>{
        console.log(res);
        this.tree=res.data
      })
    },
  },

  mounted() {
    this.getdepts()
  }
}

</script>

<style scoped>

</style>